<!--
 * @Autor: CQJ
 * @Date: 2022-08-19 13:51:08
 * @LastEditors: CQJ
 * @LastEditTime: 2022-08-22 12:12:17
 * @Description:
-->
<template>
  <div
    class="moduleList"
    v-for="(item, index) in list"
    :key="item.id"
    draggable="true"
    @dragstart="dragstart(item)"
    @dragenter="dragenter(item)"
    @dragend="dragend(item)"
  >
    <div class="moduleList-siteInfo">
      <div class="moduleList-siteInfo-topInfo">
        <div class="moduleList-siteInfo-topInfo-leftIcon">
          <el-image
            style="width: 50px; height: 50px"
            :src="item.icon"
            fit="fill"
          />
        </div>
        <div class="moduleList-siteInfo-topInfo-rightText">
          <div class="moduleList-siteInfo-topInfo-rightText-title">
            {{ item.name }}
          </div>
          <div class="moduleList-siteInfo-topInfo-rightText-content">
            <div class="moduleList-siteInfo-topInfo-rightText-content-desc">
              <span>ID:</span>
              <div>{{ item.id }}</div>
            </div>
            <div class="moduleList-siteInfo-topInfo-rightText-content-desc">
              <span>类型:</span>
              <div>{{ item.type == 1 ? '内部资源' : '外部资源' }}</div>
            </div>
            <div class="moduleList-siteInfo-topInfo-rightText-content-desc">
              <span>创建时间：</span>
              <div>{{ item.createTime }}</div>
            </div>
            <div class="moduleList-siteInfo-topInfo-rightText-content-desc">
              <span>创建人：</span>
              <div>{{ item.creator }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="moduleList-siteInfo-menu">
        <div class="moduleList-siteInfo-menu-item" @click="contentManage(item)">
          <el-icon><Menu /></el-icon>管理内容
        </div>
        <div class="moduleList-siteInfo-menu-item">
          <el-icon><Operation /></el-icon
          ><el-icon :class="getColor(index, 'left')"><SortUp /></el-icon
          ><el-icon :class="getColor(index, 'right')"><SortDown /></el-icon>排序
        </div>
        <div class="moduleList-siteInfo-menu-item">
          <el-switch
            v-model="item.status"
            class="ml-2"
            :inactive-value="0"
            :active-value="1"
            style="
              --el-switch-on-color: #13ce66;
              --el-switch-off-color: #ff4949;
            "
            :before-change="beforeChange"
          />{{ item.status ? '启用中' : '禁用' }}
        </div>
        <div class="moduleList-siteInfo-menu-item">
          <el-icon><Edit /></el-icon>编辑
        </div>
        <div class="moduleList-siteInfo-menu-item">
          <el-icon><Delete /></el-icon>删除
        </div>
      </div>
    </div>
    <div class="moduleList-numberInfo">
      <div class="moduleList-numberInfo-item">
        <div class="moduleList-numberInfo-item-title">
          <el-icon><VideoCameraFilled /></el-icon>影片数量
        </div>
        <div class="moduleList-numberInfo-item-num">{{ item.filmNumber }}</div>
      </div>
      <div class="moduleList-numberInfo-item">
        <div class="moduleList-numberInfo-item-title">
          <el-icon><PictureFilled /></el-icon>图集数量
        </div>
        <div class="moduleList-numberInfo-item-num">{{ item.imgNumber }}</div>
      </div>
      <div class="moduleList-numberInfo-item">
        <div class="moduleList-numberInfo-item-title">
          <el-icon><Document /></el-icon>文章数量
        </div>
        <div class="moduleList-numberInfo-item-num">
          {{ item.articleNumber }}
        </div>
      </div>
      <div class="moduleList-numberInfo-item">
        <div class="moduleList-numberInfo-item-title">
          <el-icon><TrendCharts /></el-icon>采集中任务
        </div>
        <div class="moduleList-numberInfo-item-num">{{ item.taskNumber }}</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { SiteItem } from '@/types/api/site/site';
import {
  Menu,
  Edit,
  Delete,
  SortUp,
  SortDown,
  Operation,
  VideoCameraFilled,
  PictureFilled,
  Document,
  TrendCharts
} from '@element-plus/icons-vue';
import { ref, computed, toRefs } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import useStore from '@/store';
import router from '@/router';
const { permission } = useStore();
interface siteData {
  list: SiteItem[];
}
const props = defineProps<siteData>();
const { list } = toRefs(props);
const clickVal = ref<SiteItem>();
const moveVal = ref<SiteItem>();
const endVal = ref<SiteItem>();
const dragstart = (item: SiteItem): void => {
  clickVal.value = item;
};
const dragenter = (item: SiteItem): void => {
  moveVal.value = item;
};
const dragend = (item: SiteItem): void => {
  endVal.value = item;
  let index = list.value.indexOf(item); //移动元素的当前位置
  let moveObj = moveVal.value ? moveVal.value : '';
  let addIndex = list.value.indexOf(moveObj as SiteItem); //要移动后的位置
  list.value.splice(index, 1);
  list.value.splice(addIndex, 0, item);
};
const contentManage = (item: SiteItem) => {
  console.log('item: ', item.name);
  permission.setSiteName(item.name);
  router.push('/system/user');
};
const beforeChange = (): any => {
  ElMessageBox.confirm(
    '禁用站点可能导致使用该站点的应用内容为空，确定要禁用吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage({
      type: 'success',
      message: '禁用成功'
    });
  });
};
//computed
const getColor = computed(() => (index: number, type: string) => {
  if (type == 'left') {
    if (index == 0) {
      return 'gray';
    } else {
      return 'red';
    }
  } else {
    if (index === list.value.length - 1) {
      return 'gray';
    } else {
      return 'green';
    }
  }
});
</script>

<style lang="scss" scoped>
.red {
  color: #ff4949;
}
.gray {
  color: #666;
}
.green {
  color: #13ce66;
}
.moduleList {
  cursor: pointer;
  width: 100%;
  height: 150px;
  border: 1px solid #eee;
  display: flex;
  border-radius: 5px;
  margin: 0 0 10px 0;
  &-siteInfo {
    width: 50%;
    box-sizing: border-box;
    border-right: 1px solid #eee;
    &-topInfo {
      width: 100%;
      height: 70%;
      padding: 15px;
      border-bottom: 1px solid #eee;
      display: flex;
      align-items: center;
      overflow: hidden;
      &-leftIcon {
        padding: 10px;
        margin-right: 10px;
        .el-image {
          border-radius: 5px;
        }
      }
      &-rightText {
        height: 50px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        &-title {
          font-size: 16px;
          font-weight: bold;
        }
        &-content {
          font-size: 14px;
          color: #666;
          display: flex;
          align-items: flex-end;
          justify-content: space-between;
          &-desc {
            display: flex;
            margin: 0 5px;
            align-items: flex-end;
            justify-content: flex-start;
            line-height: 1;
            span {
              width: auto;
            }
            div {
              flex: 1;
              line-height: 1;
              overflow: hidden; //超出的文本隐藏
              text-overflow: ellipsis; //用省略号显示
              white-space: nowrap; //不换行
            }
          }
        }
      }
    }
    &-menu {
      height: 30%;
      display: flex;
      align-items: center;
      justify-content: center;
      &-item {
        width: 25%;
        display: flex;
        align-items: end;
        justify-content: center;
        font-size: 14px;
        .el-icon {
          font-size: 20px;
        }
        .el-switch {
          height: auto;
        }
      }
    }
  }
  &-numberInfo {
    width: 50%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    &-item {
      &-title {
        align-items: flex-end;
        display: flex;
        .el-icon {
          font-size: 20px;
        }
      }
      &-num {
        font-weight: bold;
        text-align: center;
        margin-top: 5px;
      }
    }
  }
}
</style>
